<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { userApi } from '../api/index'
import { Menu } from '@element-plus/icons-vue'

const router = useRouter()

const loginForm = ref({
  username: '',
  password: ''
})

const loading = ref(false)
const rules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
}

const loginFormRef = ref(null)

const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  await loginFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        // 直接使用用户名和密码，而不是整个表单对象
        const username = loginForm.value.username
        const password = loginForm.value.password
        console.log('登录请求:', { username, password })
        
        const res = await userApi.login(username, password)
        console.log('登录响应:', res)
        
        // 保存用户信息到 localStorage
        localStorage.setItem('user', JSON.stringify(res.data))
        
        // 触发自定义事件，通知 App.vue 更新用户状态
        window.dispatchEvent(new Event('user-login'))
        
        ElMessage.success('登录成功')
        
        // 延迟跳转，确保状态更新
        setTimeout(() => {
          router.push('/')
        }, 100)
      } catch (error) {
        console.error('登录失败:', error)
        if (error.response) {
          console.error('错误响应:', error.response)
          ElMessage.error(error.response.data?.message || '登录失败，请检查用户名和密码')
        } else if (error.request) {
          console.error('无响应:', error.request)
          ElMessage.error('服务器无响应，请检查网络连接')
        } else {
          console.error('请求错误:', error.message)
          ElMessage.error('请求错误: ' + error.message)
        }
      } finally {
        loading.value = false
      }
    }
  })
}
</script>

<template>
  <div class="login-container">
    <div class="login-header">
      <el-icon class="menu-icon" size="24">
        <Menu />
      </el-icon>
    </div>
    
    <div class="login-box">
      <div class="login-form">
        <el-form
          ref="loginFormRef"
          :model="loginForm"
          :rules="rules"
          label-position="top"
        >
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              placeholder="用户名"
              size="large"
              clearable
            />
          </el-form-item>
          
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="密码"
              size="large"
              show-password
              clearable
              @keyup.enter="handleLogin"
            />
          </el-form-item>
          
          <el-form-item>
            <el-button
              type="primary"
              :loading="loading"
              class="login-button"
              size="large"
              @click="handleLogin"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
        
        <div class="register-link">
          <router-link to="/register">注册</router-link>
        </div>
      </div>
    </div>
    
    <div class="login-footer">
      &copy; 2025 会议室系统
    </div>
  </div>
</template>

<style scoped>
.login-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  background-color: #f0f2f5;
}

.login-header {
  display: flex;
  align-items: center;
  padding: 10px 20px;
  background-color: #fff;
}

.menu-icon {
  cursor: pointer;
}

.login-box {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
}

.login-form {
  width: 350px;
  background-color: #fff;
  border-radius: 4px;
  padding: 30px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.login-button {
  width: 100%;
  margin-top: 10px;
}

.register-link {
  text-align: right;
  margin-top: 15px;
}

.register-link a {
  color: #409EFF;
  text-decoration: none;
}

.login-footer {
  text-align: center;
  padding: 10px 0;
  color: #909399;
  font-size: 12px;
}
</style> 